package qualomelhor.gwt.modules.client;

import qualomelhor.database.Avatar;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;

public class AvatarWidget extends Composite {

	private final AbsolutePanel container;
	public static final int NUM_CABELOS = 2;
	public static final int NUM_OLHOS = 4;
	public static final int NUM_BOCAS = 4;
	public static final int NUM_ROUPAS = 4;

	private Image imgBody;
	private Image imgCabelo;
	private Image imgOlhos;
	private Image imgBoca;
	private Image imgRoupa;

	public AvatarWidget() {
		container = new AbsolutePanel();
		container.getElement().setId("playn-root");
		initWidget(container);
		container.setWidth("228px");
		container.setHeight("186px");
		//container.setBorderWidth(2);
	}

	public AvatarWidget(Avatar avatar) {
		this();
		carregaImagem(avatar);
	}
	
	public void carregaImagem(Avatar avatar) {
		//container.clear();
		/*
		imgBody = new Image("/images/avatar/corpo/corpo.png");
		subContainerBody.add(imgBody);

		final LayoutPanel spCabelo = new LayoutPanel();
		String pathCabelo = "/images/avatar/cabelo/cabelo" + avatar.getCodParteCabelo() + ".png";
		System.out.println("pathCabelo = " + pathCabelo);
		imgCabelo = new Image(pathCabelo);
		imgCabelo.addLoadHandler(new LoadHandler() {
			@Override
			public void onLoad(LoadEvent event) {
				Image i = (Image)event.getSource();
				i.setWidth(event.getRelativeElement().getOffsetWidth() + "px");
				i.getParent().setWidth(i.getWidth() + "px");
				
				System.out.println("imagem carregada width = " + i.getWidth());
				spCabelo.setWidgetHorizontalPosition(i, Layout.Alignment.STRETCH);
				//spCabelo.setWidgetPosition(i, (spCabelo.getOffsetWidth() - i.getWidth()) / 2, 0);
			}
		});
		//imgCabelo.setStylePrimaryName("bordaFina");
		spCabelo.add(imgCabelo);
		spCabelo.setStylePrimaryName("bordaFina");
		//subContainerPartes.setWidget(0, 0, spCabelo);
		subContainerPartes.add(spCabelo);
		subContainerPartes.setCellHorizontalAlignment(spCabelo, HasHorizontalAlignment.ALIGN_CENTER);
		System.out.println("Largura Cabelo = " + imgCabelo.getOffsetWidth() + ", spCabelo = " + spCabelo.getOffsetWidth());

		imgOlhos = new Image("/images/avatar/olho/olhos" + avatar.getCodParteOlho() + ".png");
		subContainerPartes.add(imgOlhos);

		imgBoca = new Image("/images/avatar/boca/boca" + avatar.getCodParteBoca() + ".png");
		subContainerPartes.add(imgBoca);

		imgRoupa = new Image("/images/avatar/roupa/roupa" + avatar.getCodParteRoupa() + ".png");
		subContainerPartes.add(imgRoupa);
		//subContainerPartes.setCellHorizontalAlignment(imgRoupa, HasHorizontalAlignment.ALIGN_CENTER);

		imgBody.getElement().getStyle().setZIndex(0);
		System.out.println("body z-index = " + imgBody.getElement().getStyle().getZIndex());
		System.out.println("boca z-index = " + imgBoca.getElement().getStyle().getZIndex());
		*/
	}
	
	public void carregaAvatarRandomico() {
		Avatar av = new Avatar(1, ClientUtils.getRandom(Avatar.ParteCorpo.NUM_CABELOS.getNumPartes()), ClientUtils.getRandom(Avatar.ParteCorpo.NUM_SOMBRANCELHAS.getNumPartes()), ClientUtils.getRandom(Avatar.ParteCorpo.NUM_OLHOS.getNumPartes()), ClientUtils.getRandom(Avatar.ParteCorpo.NUM_BOCAS.getNumPartes()), ClientUtils.getRandom(Avatar.ParteCorpo.NUM_ROUPAS.getNumPartes()), ClientUtils.getRandom(Avatar.ParteCorpo.NUM_MAOS.getNumPartes()));
		carregaImagem(av);
	}

	@Override
	protected void onLoad() {
		super.onLoad();
	}
}